var colorAdminApp = angular.module('colorAdminApp', [
    'ui.router',
    'ui.bootstrap',
    'oc.lazyLoad',
    'easy.ng.pagination',
]);

/**
 * 确保HttpInterceptor中能取到$rootScope,$state,$stateParams
 */
colorAdminApp.run(['$rootScope','$state','$stateParams',function($rootScope,$state,$stateParams){
	$rootScope.$state=$state;
	$rootScope.$stateParams=$stateParams;
}]);




//配置为正常的http请求而不是包装后的请求
colorAdminApp.config(function($httpProvider) {
    $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
    $httpProvider.defaults.headers.post['X-Requested-With']='XMLHttpRequest';//让后台可以识别是异步请求
    $httpProvider.defaults.headers.put['X-Requested-With']='XMLHttpRequest';//让后台可以识别是异步请求
    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data) {
           var param = function(obj) {
            var query = '';
            var name, value, fullSubName, subName, subValue, innerObj, i;
 
            for (name in obj) {
                value = obj[name];
 
                if (value instanceof Array) {
                    for (i = 0; i < value.length; ++i) {
                        subValue = value[i];
                        fullSubName = name + '[' + i + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                } else if (value instanceof Object) {
                   for (subName in value) {
                        subValue = value[subName];
                        innerObj = {};
                        innerObj[subName] = subValue;//此处实现和jquery post方法提交值相同的方式
                        query += param(innerObj) + '&';
                    }
                	
                } else if (value !== undefined && value !== null) {
                    query += encodeURIComponent(name)+'='+ encodeURIComponent(value) + '&';
                }
            }
 
            return query.length ? query.substr(0, query.length - 1) : query;
        };
        return angular.isObject(data) && String(data) !== '[object File]'? param(data): data;
    }];
});

//配置可以动态加载控制器
colorAdminApp.config(["$provide", "$compileProvider", "$controllerProvider", "$filterProvider",
           
    function ($provide, $compileProvider, $controllerProvider, $filterProvider) {
       
     colorAdminApp.controller = $controllerProvider.register;
       
     colorAdminApp.directive = $compileProvider.directive;
       
     colorAdminApp.filter = $filterProvider.register;
       
     colorAdminApp.factory = $provide.factory;
       
     colorAdminApp.service = $provide.service;
       
     colorAdminApp.constant = $provide.constant;
       
}]);

colorAdminApp.config(['$stateProvider', '$urlRouterProvider', function($stateProvider,$urlRouterProvider) {
	
    $urlRouterProvider.otherwise('/login');
  
    $stateProvider.state("login",{
        url: "/login",
        templateUrl: "login.html",
        resolve: {
            deps:["$ocLazyLoad",function($ocLazyLoad){
                  return $ocLazyLoad.load("resources/js/controller/LoginController.js");
                  }],
            service: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load({
                    files: ['resources/js/service/LoginService.js'
                    ] 
                });
            }]
        }
    });
    
   
      $stateProvider.state("main",{
        url: "/main",
        controller:"indexController",
        templateUrl: "view/i/index.html",
        resolve: {
            deps:["$ocLazyLoad",function($ocLazyLoad){
                  return $ocLazyLoad.load("resources/js/controller/i/IndexController.js");
                  }],
            service: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load({
                    files: [
                    ] 
                });
            }]
        }
    });
      $stateProvider.state("main.userPasswordUpdate",{
          url: "/core/user/passwordUpdate",
          controller:"passwordUpdateController",
          templateUrl:"view/core/user/password_update.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/user/PasswordUpdateController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: ['resources/js/service/ModalService.js',
                                    'resources/js/service/core/PasswordService.js'
                            ] 
                        });
              }]
          }
      });
      
      $stateProvider.state("main.userInfoUpdate",{
          url: "/core/user/infoUpdate",
          controller:"userInfoUpdateController",
          templateUrl:"view/core/user/user_info_update.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/user/UserInfoUpdateController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: ['resources/js/service/ModalService.js'
                            ] 
                        });
              }]
          }
      });
      
      $stateProvider.state("userPasswordUpdateForExpired",{
          url: "/core/user/passwordUpdateForExpired/:username",
          controller:"passwordUpdateForExpiredController",
          templateUrl:"view/core/user/password_update_for_expired.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/user/PasswordUpdateForExpiredController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: ['resources/js/service/ModalService.js',
                                    'resources/js/service/LoginService.js',
                                    'resources/js/service/core/PasswordService.js'
                            ] 
                        });
              }]
          }
      });
      
      $stateProvider.state("errors",{
          url: "/errors/:code",
          controller:"errorsController",
          templateUrl:"view/errors/error.html", 
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                  return $ocLazyLoad.load("resources/js/controller/errors/ErrorsController.js");
                  }]
        }
      });
      
      $stateProvider.state("main.roleManage",{
          url: "/core/role/manage",
          controller:"roleController",
          templateUrl:"view/core/role/manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/role/RoleController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: ['resources/js/service/ModalService.js',
                                    'assets/plugins/DataTables/media/css/dataTables.bootstrap.min.css',
                                    'assets/plugins/DataTables/extensions/Responsive/css/responsive.bootstrap.min.css',
                                    'plugin/ztree/css/zTreeStyle/zTreeStyle.css',
                                    'plugin/ztree/js/jquery.ztree.excheck-3.5.js',
                                    'plugin/blockui/jquery.blockUI.js'
                            ] 
                        });
              }]
          }
      });
      
      $stateProvider.state("main.resourcesManage",{
          url: "/core/resources/:type/manage",
          controller:"resourcesManageController",
          templateUrl:"view/core/resources/manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/resources/ResourcesManageController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: ['resources/js/service/ModalService.js',
                                    'plugin/ztree/css/zTreeStyle/zTreeStyle.css',
                                    'plugin/select2/dist/css/select2.min.css',
                                    'plugin/ztree/js/jquery.ztree.excheck-3.5.js',
                                    'plugin/ztree/js/jquery.ztree.exedit-3.5.js',
                                    'plugin/select2/dist/js/select2.min.js',
                                    'plugin/blockui/jquery.blockUI.js'
                            ] 
                        });
              }]
          }
      });
      
      $stateProvider.state("main.departmentManage",{
          url: "/core/org/department/manage",
          controller:"departmentManageController",
          templateUrl:"view/core/org/department_manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/org/DepartmentManageController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: ['resources/js/service/ModalService.js',
                                    'plugin/ztree/css/zTreeStyle/zTreeStyle.css',
                                    'plugin/ztree/js/jquery.ztree.excheck-3.5.js',
                                    'plugin/ztree/js/jquery.ztree.exedit-3.5.js',
                                    'plugin/blockui/jquery.blockUI.js'
                            ] 
                        });
              }]
          }
      });
      
      $stateProvider.state("main.organizationManage",{
          url: "/core/org/manage",
          controller:"organizationManageController",
          templateUrl:"view/core/org/organization_manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/org/OrganizationManageController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: ['resources/js/service/ModalService.js',
                                    'plugin/ztree/css/zTreeStyle/zTreeStyle.css',
                                    'plugin/ztree/js/jquery.ztree.excheck-3.5.js',
                                    'plugin/ztree/js/jquery.ztree.exedit-3.5.js',
                                    'plugin/blockui/jquery.blockUI.js'
                            ] 
                        });
              }]
          }
      });
      
      
      $stateProvider.state("main.userManage",{
          url: "/core/user/manage",
          controller:"userManageController",
          templateUrl:"view/core/user/manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/user/UserManageController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: ['resources/js/service/ModalService.js',
                                    'assets/plugins/DataTables/media/css/dataTables.bootstrap.min.css',
                                    'assets/plugins/DataTables/extensions/Responsive/css/responsive.bootstrap.min.css',
                                    'plugin/ztree/css/zTreeStyle/zTreeStyle.css',
                                    'plugin/ztree/js/jquery.ztree.excheck-3.5.js',
                                    'plugin/ztree/js/jquery.ztree.exedit-3.5.js',
                                    'plugin/blockui/jquery.blockUI.js'
                            ] 
                        });
              }]
          }
      });
      
      $stateProvider.state("main.systemParameterManage",{
          url: "/configuration/system/parameter/manage",
          controller:"systemParameterController",
          templateUrl:"view/configuration/system/parameter/manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/configuration/system/parameter/SystemParameterController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: ['resources/js/service/ModalService.js',
                                    'assets/plugins/DataTables/media/css/dataTables.bootstrap.min.css',
                                    'assets/plugins/DataTables/extensions/Responsive/css/responsive.bootstrap.min.css',
                                    'plugin/blockui/jquery.blockUI.js'
                            ] 
                        });
              }]
          }
      });
      
      $stateProvider.state("main.sequenceManage",{
          url: "/configuration/persistence/sequence/manage",
          controller:"sequenceController",
          templateUrl:"view/configuration/persistence/sequence/manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/configuration/persistence/sequence/SequenceController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: ['resources/js/service/ModalService.js',
                                    'assets/plugins/DataTables/media/css/dataTables.bootstrap.min.css',
                                    'assets/plugins/DataTables/extensions/Responsive/css/responsive.bootstrap.min.css',
                                    'plugin/blockui/jquery.blockUI.js'
                            ] 
                        });
              }]
          }
      });
    
}]);

colorAdminApp.controller('appController',['$rootScope', '$scope', function($rootScope, $scope) {
	 
}]);

//定义一个 Service ，稍等将会把它作为 Interceptors 的处理函数
colorAdminApp.factory('HttpInterceptor', ['$q','$injector', HttpInterceptor]);


//请求过滤器
function HttpInterceptor($q,$injector) {
	
  var rootScope=$injector.get('$rootScope');
  
  return {
    request: function(config){
      return config;
    },
    requestError: function(err){
      return $q.reject(err);
    },
    response: function(res){
    	if(res.status!=200){
    		 return res;
    	}
    	if(res.data.message=="User not login"){
    		rootScope.$status.go("login");
    	}else if(res.data.message=="Access Denied"){
    		rootScope.$status.go("main.errors",{"code":403});
    	}
        return res;
    },
    responseError: function(err){
       rootScope.$status.go("main.errors",{"code":res.status});
       return $q.reject(err);
    }
  };
}
// 添加对应的 Interceptors
colorAdminApp.config(['$httpProvider', function($httpProvider){
  $httpProvider.interceptors.push(HttpInterceptor);
}]);
colorAdminApp.controller('pageLoaderController', function($scope, $rootScope, $state) {
	App.initPageLoad();
});
